<template>
  <div class="bg-fixed"></div>

  <div class="common-layout">
    <el-container class="login-container">
      <div class="title">基于GAP的农产品质量安全全程智能系统</div>

      <div class="box">
        <el-main>
          <el-form :model="form" ref="formRef" :rules="rules" class="w-[250px]">
            <el-form-item prop="username">
              <el-input v-model="form.username" placeholder="请输入用户名">
                <template #prefix>
                  <el-icon>
                    <User />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password>
                <template #prefix>
                  <el-icon>
                    <Lock />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit" class="w-[250px]">登 录</el-button>
            </el-form-item>
          </el-form>
          <div class="goRegister">
            <a href="/#/register">没有账号？去注册</a>
          </div>
        </el-main>
      </div>

    </el-container>
  </div>
</template>

<style scoped>
template {
  margin: 0;
  padding: 0;
  overflow: hidden;
  /* 防止滚动条出现 */
}

.bg-fixed {
  background-image: url("@/assets/bg5.jpg");
  background-size: cover;
  background-attachment: fixed;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  /* 将背景图片置于最底层 */
}

.login-container {
  @apply flex items-center justify-center flex-col;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.title {
  @apply text-light-50 font-bold text-5xl;
  width: 900px;
  margin-bottom: 50px;
}

.box {
  width: 290px;
  background-color: rgba(255, 255, 255, 0.3);
  border-radius: 10px;
}

.goRegister {
  float: right;
  font-size: 12px;
  color: #fff;
}
</style>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElNotification } from 'element-plus'
import { useRouter } from 'vue-router'
// import { useCookies } from '@vueuse/integrations/useCookies'
import axios from 'axios'

const router = useRouter()

const form = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    {
      required: true,
      message: '用户名不能为空',
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: '密码不能为空',
      trigger: 'blur'
    }
  ]
}

const formRef = ref(null)

const onSubmit = () => {
  formRef.value!.validate((valid: boolean) => {
    console.log(valid)   //有输入就是true 没输入就是false
    if (valid) {
      axios.post('/api/users/login', {
        username: form.username,
        password: form.password
      })
        .then(response => {
          console.log(response.data)
          // 提示成功
          ElNotification({
            message: "登录成功",
            type: 'success',
            duration: 3000
          })

          // 存储token 和用户相关信息
          // const cookie = useCookies()
          // cookie.set("admin-token", response.data.data.token)

          // 跳转到后台首页
          router.push("/search")
        })
        .catch(error => {
          console.log('登录失败', error)
          ElNotification({
            message: error.response.data.msg || '请求失败',
            type: 'error',
            duration: 3000
          })
        })
    }
  })
}

</script>
